package fr.pud.client.controller.routes;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Route;
import fr.pud.client.model.Routes;
import fr.pud.client.view.MainFrame;
import fr.pud.utils.Tools;

/**
 * Apr 10, 2012 - tsp-osmOSE.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         SaveQueries.java
 */
public class SaveRoutes extends AbstractAction {
    /**
     * 
     */
    private static final long serialVersionUID = -3890683882140105383L;

    /**
     * Default constructor
     */
    public SaveRoutes() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.SAVE_ROUTES));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.SAVE_ROUTES));
        this.putValue(SMALL_ICON, ImagesDatabase.SAVE);
    }

    /*
     * (non-Javadoc)
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        String fileName = JOptionPane.showInputDialog(MainFrame.getSingleton(),
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.INPUT_FILE_NAME),
                LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.SAVE_ROUTES),
                JOptionPane.PLAIN_MESSAGE);
        Collection<Route> routes = Routes.getInstance().getRoutes();
        Tools.saveObject(routes, fileName, "routes");
        try {
            File fHTML = new File(
                    LanguagesConstants.LANGUAGE
                            .getString(LanguagesConstants.FOLDER_NAME_PUD_DATA)
                            + "/" + fileName + "." + "html");
            FileWriter fileOutputHTML = new FileWriter(fHTML);
            int i = 1;
            fileOutputHTML
                    .append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>");
            for (Route r : Routes.getInstance().getRoutes()) {
                fileOutputHTML.append("<h2 style=\"color:red\">"
                        + LanguagesConstants.LANGUAGE
                                .getString(LanguagesConstants.ROUTES) + " " + i
                        + " : </h2>");
                fileOutputHTML.append(r.toHTML());
                fileOutputHTML
                        .append("<br/>------------------------------------------------------------------------------------------------------------------------------------------<br/>");
                i++;
            }
            fileOutputHTML.append("</body></html>");
            fileOutputHTML.flush();
            fileOutputHTML.close();
        }
        catch (IOException e) {
            Tools.writeToLogFile("File could not be created", false,
                    e.getStackTrace());
        }
    }
}
